package cn.xej.controller;

import cn.xej.pojo.User;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author alicat.xu
 * @Date 2021/10/20 11:52 上午
 */
@RestController
public class SendController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * fanout 交换机 广播模式，routeKey 没有作用
     */
    @GetMapping("/send1")
    public void send1(){
        String exchangeName1 = "exchange.fanout.test1";
        int i = (int) (Math.random() * 10);
        rabbitTemplate.convertAndSend(exchangeName1,"","陈雨鹏抽弟弟"+i);
    }


    @GetMapping("/send2")
    public void send2(){
        String exchangeName1 = "exchange.fanout.test1";
        int i = (int) (Math.random() * 10);
        User user = new User();
        user.setId(i+"");
        user.setName("张三");
        rabbitTemplate.convertAndSend(exchangeName1,"",user);
    }
    
    @GetMapping("/send3")
    public void send3(){
        String exchangeName1 = "exchange.fanout.test";
        int i = (int) (Math.random() * 10);
        User user = new User();
        user.setId(i+"");
        user.setName("张三");
        rabbitTemplate.convertAndSend(exchangeName1,"",user);
    }


    @GetMapping("/send4/{id}")
    public void send4(@PathVariable("id")Integer id){
        String key = "abc";
        if (id == 1){
            key = "routing.A";
        }else if (id == 2){
            key = "routing.B";
        }
        String exchangeName1 = "exchange.direct.test2";
        int i = (int) (Math.random() * 10);
        User user = new User();
        user.setId(i+"");
        user.setName("张三");
        rabbitTemplate.convertAndSend(exchangeName1,key,user,new CorrelationData("1"));
    }
}
